package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.Topic;
import com.srwl.mytx.map.DateUtils;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;
import com.srwl.mytx.activity.TopicDetailsActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dahai2070 on 2016/10/6 0006.
 */
public class GoodsAdapterForLive extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int GOODS_STATUS_AUCTION = 1;
    private static final int GOODS_STATUS_NOT_AUCTION = 0;
    private List<Topic> goodsList;
    private Context mContext;
    private LayoutInflater inflater;
    public Boolean bol_owner;
    private int mHeaderCount = 1;

    public GoodsAdapterForLive(List<Topic> goodsList, Context mContext, Boolean bol_owner) {
        this.goodsList = goodsList;
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.bol_owner = bol_owner;
    }

    public interface OnItemClickListener {
        void goodsPhotoClick(View v, int position);

        void bindGoods(View v, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {

        return goodsList.get(position).getStatus();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case GOODS_STATUS_AUCTION:
                View view = inflater.inflate(R.layout.tx_goods_item_auction_live, parent, false);
                return new GoodsHolderAuction(view);
            case GOODS_STATUS_NOT_AUCTION:
                View view1 = inflater.inflate(R.layout.tx_goods_item_not_auction_live, parent, false);
                return new GoodsHolderNotAuction(view1);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final Topic goods = goodsList.get(position);
        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();
        if (holder instanceof GoodsHolderAuction) {
            GoodsHolderAuction holderAuction= (GoodsHolderAuction) holder;
           // Glide.with(mContext).load(goods.getPictures().get(0)).apply(options).into(((GoodsHolderAuction) holder).ivGoodsPhoto);
            GlideLoadUtils.glideLoad(mContext, goods.getPictures().get(0), holderAuction.ivGoodsPhoto);
            holderAuction.ivGoodsPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  onItemClickListener.goodsPhotoClick(holder.itemView,position);

                    mContext.startActivity(new Intent(mContext, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, goods));
                }
            });
            holderAuction.tvGoodsName.setText(goods.getTitle());
            holderAuction.tvAuctionPrice.setText(Utils.numberFormat(goods.getAuctionPrice()));
            holderAuction.tvNomalPrice.setText(Utils.numberFormat(goods.getPrice()));
            holderAuction.tvGoodsDescription.setText(goods.getContent());
            String showTime = DateUtils.showTime(goods.getStartTime(), goods.getStopTime());
            holderAuction.tvAuctionTime.setText(showTime);
            if (showTime.equals("拍卖已结束")) {
                holderAuction.btn_bind.setEnabled(false);
            }

            if (bol_owner) {
                holderAuction.btn_bind.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //绑定该商品到直播间
                        onItemClickListener.bindGoods(holder.itemView, position);
                        holderAuction.btn_bind.setEnabled(false);
                    }
                });
            } else {
                holderAuction.ll_bind.setVisibility(View.GONE);
            }
        } else if (holder instanceof GoodsHolderNotAuction) {
            GoodsHolderNotAuction holderNotAuction= (GoodsHolderNotAuction) holder;
            holderNotAuction.tvGoodsName.setText(goods.getTitle());
            holderNotAuction.tvNomalPrice.setText(Utils.numberFormat(goods.getPrice()));


            //Glide.with(mContext).load(goods.getPictures().get(0)).apply(options).into(((GoodsHolderNotAuction) holder).ivGoodsPhoto);
            GlideLoadUtils.glideLoad(mContext, goods.getPictures().get(0), holderNotAuction.ivGoodsPhoto);
            holderNotAuction.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mContext.startActivity(new Intent(mContext, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, goods));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return goodsList == null ? 0 : goodsList.size();
    }


    class GoodsHolderAuction extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_goods_photo)
        ImageView ivGoodsPhoto;
        @BindView(R.id.tv_title)
        TextView tvGoodsName;
        @BindView(R.id.tv_auction_price)
        TextView tvAuctionPrice;
        @BindView(R.id.tv_nomal_price)
        TextView tvNomalPrice;
        @BindView(R.id.tv_auction_time)
        TextView tvAuctionTime;
        @BindView(R.id.tv_goods_description)
        TextView tvGoodsDescription;
        @BindView(R.id.btn_bind)
        Button btn_bind;

        @BindView(R.id.ll_bind)
        LinearLayout ll_bind;

        public GoodsHolderAuction(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    class GoodsHolderNotAuction extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_goods_photo)
        ImageView ivGoodsPhoto;
        @BindView(R.id.tv_title)
        TextView tvGoodsName;

        @BindView(R.id.tv_nomal_price)
        TextView tvNomalPrice;

        public GoodsHolderNotAuction(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


}